const tree1 = {
  val: 1,
  left: {
    val: 2,
    left: {
      val: 4,
      left: null,
      right: null,
    },
    right: {
      val: 5,
      left: null,
      right: null,
    },
  },
  right: {
    val: 3,
    left: {
      val: 6,
      left: null,
      right: null,
    },
    right: null,
  },
};

const tree2 = {
  val: 1,
  left: {
    val: 2,
    left: {
      val: 4,
      left: null,
      right: null,
    },
    right: {
      val: 5,
      left: null,
      right: null,
    },
  },
  right: {
    val: 3,
    left: {
      val: 6,
      left: null,
      right: null,
    },
    right: null,
  },
};

var isSameTree = function (p, q) {
  const tool = (l, r) => {
    if (!l && !r) {
      return true;
    }
    if (l && r && l.val === r.val) {
      return tool(l.left, r.left) && tool(l.right, r.right);
    }
    return false;
  };
  return tool(p, q);
};

console.log(isSameTree(tree1, tree2));
